package com.example.myapplication.activity;

import android.content.Intent;
import android.os.Bundle;
//import android.support.v4.view.MenuItemCompat;
//import android.support.v7.app.AppCompatActivity;
//import android.support.v7.widget.LinearLayoutManager;
//import android.support.v7.widget.RecyclerView;
//import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.adapters.HorizontalAdapter;
import com.example.myapplication.adapters.VerticalAdapter;
import com.example.myapplication.model.Food;
import com.example.myapplication.model.GeneralFood;
import com.example.myapplication.rest.RetrofitClient;
import com.example.myapplication.rest.RetrofitInterface;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.MenuItemCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ShopActivity extends AppCompatActivity {

    RecyclerView recyclerViewHorizontal;
    RecyclerView recyclerViewVertical;
    public static TextView tv;
    public static List<GeneralFood> cartFoods = new ArrayList<>();
    public Toolbar toolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);

        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setTitle("食物类别");
        toolbar.setTitleTextColor(0xFFFFFFFF);
        cartUpdate();

        recyclerViewHorizontal = findViewById(R.id.horizontal_recyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        recyclerViewHorizontal.setLayoutManager(linearLayoutManager);

        recyclerViewVertical = findViewById(R.id.vertical_recyclerview);
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerViewVertical.setLayoutManager(linearLayoutManager2);

        RetrofitInterface retrofitService = RetrofitClient.getClient().create(RetrofitInterface.class);

        Call<Food> call = retrofitService.getFoods();
        final List<GeneralFood> popularFoods = new ArrayList<>();
        final List<GeneralFood> regularFoods = new ArrayList<>();

        String[] popularFoodsimgs = {"https://i.loli.net/2021/06/24/UKjIrNBgbRXQLCS.jpg",
                "https://i.loli.net/2021/06/24/i8nmaGvoUPCcHl2.jpg",
                "https://i.loli.net/2021/06/24/mq6A5QSo1Fwu7CM.jpg",
                "https://i.loli.net/2021/06/24/vPrtfRSWUzT1NXo.jpg",
                "https://i.loli.net/2021/06/24/MWJUeXVdhNisQTa.jpg"
        };
        double[] popularFoodsprice = {11.0,12.0,13.0,10.0,11.5,11.0};
        String[] popularFoodsname = {"冰淇淋旋风","咖啡","奶茶","柠檬茶","可乐"};
        String[] popularFoodsdes = {"夏日经典冰淇淋","经典摩卡咖啡","经典顶级奶茶","清凉一夏","经典可乐天下无敌"};
        int[] popularFoodsCalory = {400,500,600,300,200};

        String[] regularFoodsimgs = {
            "https://i.loli.net/2021/06/24/s5fUi9enyYCp8gj.jpg",
            "https://i.loli.net/2021/06/24/JSVpnXuLFHa4jOc.jpg",
            "https://i.loli.net/2021/06/24/WP7spkXfMwFh32Q.jpg",
            "https://i.loli.net/2021/06/24/p1ECYB7ZjkqfGbA.jpg",
            "https://i.loli.net/2021/06/23/Duwl3ZRorVvK9Ma.jpg",
            "https://i.loli.net/2021/06/23/I5xshw6TA3XNB4e.jpg",
            "https://i.loli.net/2021/06/23/HWBL5h16fPpDlAv.jpg",
            "https://i.loli.net/2021/06/23/c8vsEfVRkmKtHAD.jpg",
            "https://i.loli.net/2021/06/24/RpcEz7TwVXKlmkQ.jpg",
            "https://i.loli.net/2021/06/23/YS6dlKwhAX5Wm7u.jpg",
                "https://i.loli.net/2021/06/24/TqJgRCcsMZQh8xd.jpg",
                "https://i.loli.net/2021/06/24/jPlW4omDSpcU9gT.jpg"
        };
        double[] regularFoodsprice = {41.99,42.19,23.99,30.00,18.99,17.00,19.99,17.66,18.66,19.00,9.99};
        String[] regularFoodsname = {"套餐A","套餐B","套餐C","套餐D","芝士培根鸡蛋堡","芝士牛肉堡","奥尔良烤鸡腿堡","巨无霸汉堡","新奥尔良鸡翅","芝士肉饼堡","鸡肉沙拉","鸡胸肉沙拉"};
        String[] regularFoodsdes = {"6块奥尔良鸡翅+薯条","6块香辣鸡翅+薯条","薯条+可乐","薯条+橙汁+布丁",
            "芝士培根鸡蛋堡","芝士牛肉堡","奥尔良烤鸡腿堡","双层牛肉","新奥尔良鸡翅","芝士肉饼堡","蔬菜与鸡肉的灵魂组合","只不过换成了鸡胸肉你就想买？"
        };
        int[] regularFoodsCalory = {400,500,600,300,200,320,330,400,600,300,700,300};
        for(int i = 0;i < 5;i++){
            GeneralFood gf = new GeneralFood();
            gf.setImage(popularFoodsimgs[i]);
            gf.setPrice(popularFoodsprice[i]);
            gf.setDescription(popularFoodsdes[i]);
            gf.setCalory(popularFoodsCalory[i]);
            gf.setTitle(popularFoodsname[i]);
            popularFoods.add(gf);
        }
        for(int i = 0;i < 11;i++){
            GeneralFood gf = new GeneralFood();
            gf.setImage(regularFoodsimgs[i]);
            gf.setPrice(regularFoodsprice[i]);
            gf.setDescription(regularFoodsdes[i]);
            gf.setCalory(regularFoodsCalory[i]);
            gf.setTitle(regularFoodsname[i]);
            regularFoods.add(gf);
        }

        call.enqueue(new Callback<Food>() {
            @Override
            public void onResponse(Call<Food> call, Response<Food> response) {
                recyclerViewHorizontal.setAdapter(new HorizontalAdapter(popularFoods, R.layout.recyclerview_horizontal, ShopActivity.this));
                recyclerViewVertical.setNestedScrollingEnabled(false);
                recyclerViewVertical.setAdapter(new VerticalAdapter(regularFoods, R.layout.recyclerview_vertical, getApplicationContext()));
            }
            @Override
            public void onFailure(Call<Food> call, Throwable t) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);

        cartUpdate();
        MenuItem item = menu.findItem(R.id.action_addcart);
        MenuItemCompat.setActionView(item, R.layout.cart_count_layout);
        RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(item);
        tv = notifCount.findViewById(R.id.hotlist_hot);
        View view = notifCount.findViewById(R.id.hotlist_bell);

        cartUpdate();

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(ShopActivity.this, CartActivity.class);
                startActivity(myIntent);


        }});

        return true;
    }

    public static void cartUpdate() {
        if (tv != null && cartFoods != null)
            tv.setText(Integer.toString(cartFoods.size()));
    }

    @Override
    protected void onResume() {
        invalidateOptionsMenu();
        super.onResume();
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}

